package com.blog.controller.admin;

import com.blog.entity.BlogType;
import com.blog.entity.PageBean;
import com.blog.service.BlogService;
import com.blog.service.BlogTypeService;
import com.blog.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//博客类型管理
@Controller
@RequestMapping("/admin/blogType")
public class BlogTypeAdminController {

    @Resource
    private BlogTypeService blogTypeService;
    @Resource
    private BlogService blogService;

    //博客标签列表
    @RequestMapping("/list")
    public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, HttpServletResponse response) throws Exception {

        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        //查询博客类型列表
        List<BlogType> blogTypeList = blogTypeService.list(map);
        //查询总共有几条数据
        Long total = blogTypeService.getTotal(map);
        //将数据转换为json对象
        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(blogTypeList);
        result.put("rows", jsonArray);
        result.put("total", total);
        //将数据写入response
        ResponseUtil.write(response, result);

        return null;
    }

    //保存博客标签信息
    @RequestMapping("/save")
    public String save(BlogType blogType, HttpServletResponse response) throws Exception {

        int resultTotal = 0;

        if (blogType.getId() == null) {
            resultTotal = blogTypeService.add(blogType);//如果标签信息为空则新增
        } else {
            resultTotal = blogTypeService.update(blogType);//如果标签信息为不空则修改
        }

        //创建json对象，把数据传给前台
        JSONObject result = new JSONObject();

        if (resultTotal > 0) {
            result.put("success", true);
        } else {
            result.put("false", false);
        }

        ResponseUtil.write(response, result);

        return null;
    }

    //删除博客标签
    @RequestMapping({"/delete"})
    public String delete(@RequestParam("ids") String ids, HttpServletResponse response) throws Exception {

        String[] idsStr = ids.split(",");

        JSONObject result = new JSONObject();

        for (int i = 0; i < idsStr.length; i++) {
             if (blogService.getBlogByTypeId(Integer.valueOf(idsStr[i])) > 0) {
                 result.put("exist", "此博客标签下有文章，不能删除！");
             } else {
                blogTypeService.delete(Integer.valueOf(idsStr[i]));
            }
        }

        result.put("success", true);

        ResponseUtil.write(response, result);

        return null;
    }
}

